package cn.m3a1.control.admin;

import cn.m3a1.bean.Curse;
import cn.m3a1.bean.Msg;
import cn.m3a1.bean.Tclass;
import cn.m3a1.bean.User;
import cn.m3a1.service.admin.AdminCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wyj
 * @date 2019/7/6
 * @time 上午8:36
 */
@Controller
@RequestMapping("/admin")
public class AdminCourseController {
    @Autowired
    AdminCourseService adminCourseService;

    @RequestMapping("/coursehome")
    public String coursehome(ModelMap modelMap) {
        List<Curse> courceAll = adminCourseService.getCourceAll();
        List<User> allTeacher = adminCourseService.getAllTeacher();
        List<Tclass> allTclass = adminCourseService.getAllTclass();
        modelMap.put("teachers",allTeacher);
        modelMap.put("tclasses",allTclass);
        modelMap.put("members",courceAll);
        return "Admin/course";
    }
    @ResponseBody
    @RequestMapping("/curseSave")
    public Msg curseSave(Curse curse){
        adminCourseService.saveCurse(curse);

        return Msg.success();
    }

    /**
     * 提交更新课程信息
     * @param curse
     * @return
     */
    @ResponseBody
    @RequestMapping("/curseUp")
    public  Msg curseUp(Curse curse){
        adminCourseService.upCurse(curse);
        return Msg.success();
    }

    /**
     * 提交删除信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delCurse")
    public  Msg delCurse(int id){
        adminCourseService.delCurse(id);
        return  Msg.success();
    }
    @ResponseBody
    @RequestMapping("/getCurseTId")
    public Msg getCurseTId(int id){
        List<Curse> curses= adminCourseService.getCurse(id);
        if (curses.isEmpty()){
            Msg.fail();
        }
        return Msg.success().add("course",curses.get(0));
    }

}
